package projectmanagement.managementtool;

import java.util.*;

public class IdGenerator {
    private static volatile IdGenerator instance = null;
    private static Map<String, Integer> ids = new HashMap<String, Integer>();

    private IdGenerator() {}

    public static IdGenerator getInstance() {
        IdGenerator localInstance = instance;
        if (localInstance == null) {
            synchronized (IdGenerator.class) {
                localInstance = instance;
                if (localInstance == null) {
                    instance = localInstance = new IdGenerator();
                }
            }
        }
        return localInstance;
    }

    public static int generate(Class<?> cl) {
        String className = cl.toString();
        int newId = 0;
        synchronized (IdGenerator.class) {
            if(ids.containsKey(className))
                newId = ids.get(className) + 1;
            ids.put(className, newId);
        }
        return newId;
    }

//    public static int generate(Class<?> cl) {
//        String className = cl.toString();
//        int newId = ids.containsKey(className) ? ids.get(className) + 1 : 0;
//        ids.put(className, newId);
//        return newId;
//    }

    public static int getLastId(Class<?> cl){
        String className = cl.toString();
        return ids.containsKey(className) ? ids.get(className) : -1;
    }
}